package business_opportunity

import (
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/common"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

/**
表单controller
*/

type FormController struct {
	controller.BaseController
	formService common.FormServiceIface
}

func NewFormController(formService common.FormServiceIface) *FormController {
	return &FormController{
		formService: formService,
	}
}

func (f *FormController) RegisterRouter(g *gin.RouterGroup) {
	//找商机(高级筛选)
	g.GET("/get-form-template", f.GetFormTemplateConfig)
	g.GET("/get-form-data-rule", f.GetFormDataRule)

}

// GetFormTemplateConfig 获取模板配置
func (f *FormController) GetFormTemplateConfig(ctx *gin.Context) {
	templateId := ctx.Request.Header.Get("template_id")
	//配置化模板暂未实现
	templateId = ""

	data, err := f.formService.GetFormTemplateConfig(ctx, templateId)
	if err != nil {
		stark.Logger.Errorf(ctx, "获取模板配置接口出错", err.Error())
		f.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	if data == nil {
		var emptyData []string
		f.Success(ctx, emptyData)
		return
	}

	f.Success(ctx, data)
	return
}

// GetFormDataRule @Summary 获取数据校验规则
// @ID get-form-data-rule
// @Param key query string false "规则key,多个key以半角逗号分割，如果为空，则返回全部key"
// @Success 200 {object} Data
func (f *FormController) GetFormDataRule(ctx *gin.Context) {
	data, err := f.formService.GetFormDataRule(ctx, "")
	if err != nil {
		stark.Logger.Errorf(ctx, "获取数据校验规则接口出错", err.Error())
		f.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	if data == nil {
		var emptyData []string
		f.Success(ctx, emptyData)
		return
	}

	f.Success(ctx, data)
	return
}
